package com.petmall.service;

import com.petmall.entity.Cart;
import com.petmall.entity.Cart_item_info;
import com.petmall.entity.Item;
import com.petmall.mapper.CartMapper;
import com.petmall.mapper.ItemMapper;
import com.petmall.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;

/**
 * Created by pig2guang on 2017/4/19.
 */
@Service
public class CartService {

    @Autowired
    private CartMapper cartmapper;

    @Autowired
    private ItemMapper itemmapper;

    public ArrayList<Cart_item_info> listAllItemsInCart(Object user) {
        ArrayList<Cart_item_info> cart_items_info = new ArrayList<Cart_item_info>();
        for (Cart cart_item : cartmapper.selectCartByUserId(new UserUtil().getUserId(user))){
            Cart_item_info item_info = new Cart_item_info();
            Item item = itemmapper.selectItemById(cart_item.getItem_id());
            item_info.setCart_item_id(cart_item.getItem_id());
            item_info.setCart_item_num(cart_item.getNum());
            item_info.setCart_item_name(item.getName());
            item_info.setCart_item_price(item.getPrice());
            item_info.setCart_item_total_price(item.getPrice()*cart_item.getNum());
            item_info.setCart_item_image(item.getImage());
            //item_info.setCart_item_status(item.getStatus());
            //item_info.setCart_item_stock_num(item.getNum());
            System.out.print(item_info.getCart_item_name() + item_info.getCart_item_image() + item_info.getCart_item_num() + item_info.getCart_item_total_price());
            cart_items_info.add(item_info);
        }
        return cart_items_info;
    }

    public void addItemToCart(Object user, String num, String item_id){
        cartmapper.insertItem(new UserUtil().getUserId(user), Long.valueOf(item_id), Integer.parseInt(num));
    }

    public void removeItemFromCart(Object user, long item_id){
        cartmapper.deleteItemByItemId(new UserUtil().getUserId(user), item_id);
    }

//    public void updateItemNumberInCart(long user_id, long item_id, int num){
//        cartmapper.updateItemNumberInCart(user_id, item_id, num);
//    }

}
